//
//  NDCancelAccountAlertView.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/11/4.
//

import SwiftUI

struct NDCancelAccountAlertView: View {
    let title: String?
    let tips: String?
    let message: String?
    let backgroundImage: String?
    let textAlignment: TextAlignment?
    let buttons: [NDPopupButton]
    
    @State private var isPrivacyPolicyAccepted = false
    @Binding private var isSelected: Bool
    let onClickPravicy: (() -> Void)?

    init(title: String?, tips: String?, message: String?, backgroundImage: String?, isSelected: Binding<Bool>, textAlignment: TextAlignment? = .center, buttons: [NDPopupButton], onClickPravicy: (() -> Void)? = nil) {
        self.title = title
        self.tips = tips
        self.message = message
        self.backgroundImage = backgroundImage
        self.isPrivacyPolicyAccepted = isSelected.wrappedValue
        self._isSelected = isSelected
        self.textAlignment = textAlignment
        self.buttons = buttons
        self.onClickPravicy = onClickPravicy
    }
    
    // 私有常量
    private let horizontalPadding: CGFloat = 16
    private let verticalSpacing: CGFloat = 20
    private let titleTopPadding: CGFloat = 30
    private let titleMessageSpacing: CGFloat = 4
    private let buttonSpacing: CGFloat = 10
    private let buttonBottomPadding: CGFloat = 20
    private let tipsMessageSpacing: CGFloat = 10
    
    var body: some View {
        VStack(spacing: 0) {
            titleView
            messageAndbuttonSection
        }
        .frame(height: calculateViewHeight())
        .padding(.horizontal, 10)
        .padding(.bottom, 20)
    }
    
    private var titleView: some View {
        Group {
            if let title = title {
                Spacer(minLength: 10)
                Text(title)
                    .font(.system(size: 20, weight: .bold))
                    .foregroundColor(Color(hex: "#FFFFFF"))
                    .multilineTextAlignment(.leading)
                    .padding(.top, titleTopPadding)
                    .padding(.horizontal, horizontalPadding)
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .shadow(color: .black, radius: 1, x: 0, y: 0)
                    .shadow(color: .black, radius: 1, x: 0, y: 0)
                    .shadow(color: .black, radius: 1, x: 0, y: 0)
                
                Spacer()
                    .frame(height: 25)
                Spacer()
            }
        }
    }
    
    private var tipsView: some View {
        Group {
            if let tips = tips {
                Text(tips)
                    .font(.system(size: 14, weight: .medium))
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .multilineTextAlignment(.leading)
                    .foregroundColor(Color(hex: "#271F24"))
                    .padding(.horizontal, horizontalPadding)
                    .padding(.top, verticalSpacing) // 只在顶部添加内边距
            }
        }
    }
    
    private var messageView: some View {
        Group {
            if let message = message {
                Text(message)
                    .font(.system(size: 14))
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .multilineTextAlignment(.leading)
                    .foregroundColor(Color(hex: "#666666"))
                    .padding(.horizontal, horizontalPadding)
                    .padding(.vertical, tips == nil ? verticalSpacing : 0) // 如果没有tips，添加垂直内边距
                    .padding(.bottom, verticalSpacing) // 总是添加底部内边距
            }
        }
    }
    
    
    func togglePrivacyPolicy() {
        withAnimation(.easeInOut(duration: 0.2)) {
            isPrivacyPolicyAccepted.toggle()
            isSelected.toggle()
        }
    }
    
    var privacyPolicySection: some View {
        HStack(alignment: .top, spacing: 5) {
            Spacer()
            privacyCheckbox
            Text(localizedString("cancel_account_alert_pravicy"))
                .foregroundColor(Color(hex: "#999999"))
                .onTapGesture(perform: handlePrivacyTextTap)
            Spacer()
        }
    }
    
    var privacyCheckbox: some View {
        Button(action: togglePrivacyPolicy) {
            ZStack {
                Circle()
                    .stroke(
                        isPrivacyPolicyAccepted ? Color.primaryOrange : Color.grayMedium,
                        lineWidth: isPrivacyPolicyAccepted ? 4 : 2
                    )
                    .frame(width: 12, height: 12)
            }
            .frame(width: 20, height: 20, alignment: .center)
            .contentShape(Rectangle())
        }
        .buttonStyle(PlainButtonStyle())
    }
    
    func handlePrivacyTextTap() {
        onClickPravicy?()
    }
    
    // MARK: - 按钮区域
    private var messageAndbuttonSection: some View {
        VStack {
            Spacer()
            // 根据内容存在情况动态布局
            if tips != nil && message != nil {
                // 两者都存在时，使用固定间距
                VStack(spacing: tipsMessageSpacing) {
                    tipsView
                    messageView
                }
                .padding(.top, 10)
            } else {
                // 只有一个存在时，直接显示
                tipsView
                messageView
            }
            
            privacyPolicySection
            
            Spacer()
            
            if !buttons.isEmpty {
                VStack(spacing: buttonSpacing) {
                    ForEach(Array(buttons.enumerated()), id: \.offset) { index, button in
                        button
                    }
                }
                .background(Color.clear)
                .padding(.bottom, buttonBottomPadding)
                .padding(.horizontal, 16)
            }
        }
        .background(Color(hex: "#FFFFFF"))
        .cornerRadius(20)
    }
    
    // MARK: - 高度计算
    private func calculateViewHeight() -> CGFloat {
        guard let backgroundImage = backgroundImage else {
            return calculateDynamicHeight()
        }
        
        return ImageUtility.proportionalHeight(for: backgroundImage,
                                               targetWidth: AppConstants.screenWidth - 2 * horizontalPadding)
    }
    
    private func calculateDynamicHeight() -> CGFloat {
        return 500
    }
}

